﻿#include "extractdialog.h"
#include "ui_extractdialog.h"

ExtractDialog::ExtractDialog(int currentPage, int startPage, int lastPage, int greenCount, CenteredWidget *parent) :
	QDialog(parent),
	ui(new Ui::ExtractDialog)
{
	ui->setupUi(this);

	m_startPage = startPage;
	m_currentPage = currentPage;
	m_lastPage = lastPage;
	ui->currentPage->setText (tr ("de la page %1").arg (m_currentPage));
	ui->startPage->setRange (startPage, lastPage);
	ui->startPage->setValue (startPage);
	ui->lastPage->setRange (startPage, lastPage);
	ui->lastPage->setValue (lastPage);

	if (startPage == lastPage)
	{
		ui->range->setVisible (false);
		ui->startPage->setVisible (false);
		ui->lastPage->setVisible (false);
		ui->labelRange->setVisible (false);
		ui->allPages->setVisible (false);
	}

	m_group = new QButtonGroup (this);

	m_group->addButton (ui->greenSelection, 0);
	m_group->addButton (ui->currentPage, 1);
	m_group->addButton (ui->allPages, 2);
	m_group->addButton (ui->range, 3);
	if (greenCount > 0)
	{
		ui->greenSelection->setVisible (true);
		ui->greenSelection->setText (tr ("de la sélection verte"));
		ui->greenSelection->setChecked (true);
	}
	else
	{
		ui->greenSelection->setVisible (false);
		ui->currentPage->setChecked (true);
	}
	m_config = new Config (this);
	ui->optionUsePdfToText->setChecked (m_config->optionUsePdfToText ());

	connect (ui->optionUsePdfToText,
			 SIGNAL(toggled(bool)),
			 m_config,
			 SLOT(setOptionUsePdfToText(bool)));
}
//--------------------------------
ExtractDialog::~ExtractDialog()
{
	delete ui;
	delete m_config;
}
//------------------------------------
QPair<int, int> ExtractDialog::range ()
{
	QPair<int, int> result;
	switch (m_group->checkedId ())
	{
	case 0:
		result.first = -1;
		result.second = -1;
		break;

	case 1:
		result.first = m_currentPage;
		result.second = m_currentPage;
		break;

	case 2:
		result.first = m_startPage;
		result.second = m_lastPage;
		break;

	case 3:
		result.first = qMin (ui->startPage->value (), ui->lastPage->value ());
		result.second = qMax (ui->startPage->value (), ui->lastPage->value ());
		break;

	default:
		result.first = 0;
		result.second = 0;
		break;
	}

	return result;
}
//-----------------------------------------
void ExtractDialog::changeEvent(QEvent *e)
{
	QDialog::changeEvent(e);
	switch (e->type()) {
	case QEvent::LanguageChange:
		ui->retranslateUi(this);
		break;
	default:
		break;
	}
}
//----------------------------------------------------------
void ExtractDialog::on_startPage_valueChanged(int /*arg1*/)
{
	ui->range->setChecked (true);
}
//--------------------------------------------------------
void ExtractDialog::on_lastPage_valueChanged(int /*arg1*/)
{
	ui->range->setChecked (true);
}
